#ifndef WEB_H
#define WEB_H

/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include <QNetworkAccessManager>
#include <QNetworkProxy>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QByteArray>
#include <QUrl>

class QObject;
class QNetworkReply;
class QNetworkCookieJar;

enum NewsRequestReason
{
    NRR_News,
    NRR_UserOnliner,
    NRR_Inbox,
};

class WebClient : public QNetworkAccessManager
{
    Q_OBJECT

private:
    enum RequestType
    {
        RT_OnelinerRead,
        RT_QueueRead,
        RT_Login,
        RT_NewsRead,
        RT_UsersOnlineRead,
        RT_InboxRead,
        RT_StreamRead,
        RT_FavoritesRead,
        RT_PaginatedFavoritesRead,
        RT_UserFavoritesRead,
        RT_UserPaginatedFavoritesRead,
        RT_SongRead,
        RT_ArtistRead,
        RT_GroupRead,
        RT_CompilationRead,
        RT_UserRead,

        RT_OnelinerSubmission,

        RT_Vote,
        RT_FavAdd,
        RT_UnFav,
        RT_SongRequest,

        RT_Undefined,
    };

    //QUrl rootSiteUrl;
    QNetworkRequest basicRequest;

    QByteArray queueData;
    QByteArray onelinerData;
    QByteArray newsData;
    QByteArray streamData;
    QByteArray favoritesData;
    QByteArray songData;
    QByteArray artistData;
    QByteArray groupData;
    QByteArray compilationData;
    QByteArray userData;

    // Cookie support
    QNetworkCookieJar* pCookies;

    // Proxy support
    QNetworkProxy proxy;

protected slots:

    void replyFinished(QNetworkReply* reply);
    void errorHandler(QNetworkReply::NetworkError errorCode);

signals:
    void queueReady(void);
    void onelinerReady(void);
    void newsReady(void);
    void usersOnelineReady(void);
    void inboxReady(void);
    void streamReady(void);
    void favoritesReady(void);
    void paginatedFavoritesReady(void);
    void userFavoritesReady(void);
    void userPaginatedFavoritesReady(void);
    void songReady(void);
    void artistReady(void);
    void groupReady(void);
    void compilationReady(void);
    void userReady(QString username);
    void errorOccured(QString message);

    void connectionSucceed(void);
    void connectionFailed(void);

public slots:

    void requestUserPage(const QString username);

public:
    WebClient(const QUrl& rootSiteUrl, QObject* parent=0);
    ~WebClient(void);

    void login(const QString& username, const QString& password);
    void defineProxy(const QNetworkProxy::ProxyType type, const QString& hostName, const quint16 port, const QString& user="", const QString& password="");

    void requestQueue(void);
    void requestOneliner(void);
    void requestNews(const NewsRequestReason reason=NRR_News);
    void requestStream(void);
    void requestFavoritePage(const QString& username, const bool isUsersFav);
    void requestFavoritePage(const QString& username, const bool isUsersFav, const unsigned int pageNumber);
    void requestGroupPage(const unsigned int id);
    void requestCompilationPage(const unsigned int id);
    void requestVote(const unsigned int id, const short unsigned int vote);
    void requestFavSong(const unsigned int id);
    void requestUnFavSong(const unsigned int id);
    void requestRequestSong(const unsigned int id);

    QByteArray& getQueueData(void) { return queueData; }
    QByteArray& getOnelinerData(void) { return onelinerData; }
    QByteArray& getNewsData(void) { return newsData; }
    QByteArray& getStreamData(void) { return streamData; }
    QByteArray& getFavoritesData(void) { return favoritesData; }
    QByteArray& getSongData(void) { return songData; }
    QByteArray& getArtistData(void) { return artistData; }
    QByteArray& getGroupData(void) { return groupData; }
    QByteArray& getCompilationData(void) { return compilationData; }
    QByteArray& getUserData(void) { return userData; }

    QNetworkProxy::ProxyType getProxyType()const { return this->proxy.type(); }
    short unsigned int getProxyPort()const { return this->proxy.port(); }
    QString getProxyHost()const { return this->proxy.hostName(); }

public slots:
    void requestSongPage(int id);
    void requestArtistPage(int id);
    void sendOnelinerMessage(const QString& message);
};

#endif // WEB_H
